<template>
	<view class="page">
		<u-gap height="30"></u-gap>
		<view class="tabVue margin0">
			<u-subsection :list="tabs" :current="current" button-color=" #18AB4E" active-color="#fff" bg-color="#fff" @change="change"></u-subsection>
		</view>
		<template v-if="current===0">
			<view class="box margin30">
				<view class="title">生成兑换码<text>(还可生成{{totalNumber-(spNumber+wxNumber)}}个)</text></view>
				<view class="flex-between align-center margin20">
					<view class="ket">AI视频兑换码</view>
					<u-number-box v-model="spNumber" :max="totalNumber-wxNumber"></u-number-box>
				</view>
				<view class="flex-between align-center margin20">
					<view class="ket">AI微信兑换码</view>
					<u-number-box v-model="wxNumber" :max="totalNumber-spNumber"></u-number-box>
				</view>
				<view class="btn margin30" @click="generateRedemptionCode()">生成</view>
			</view>
			<view class="flex-between margin30 padding-row30">
				<view class="info flex-between flex-column align-center padding-column25">
					<text style="color: #000; font-weight: 600; font-size: 32rpx;">{{redemptionCode.videoTotal ? redemptionCode.videoUsed+"/"+redemptionCode.videoTotal:"0/0"}}</text>
					<text style="color: #5F6671;">已使用AI视频兑换码</text>
				</view>
				<view class="info flex-between flex-column align-center padding-column25">
					<text style="color: #000; font-weight: 600; font-size: 32rpx;">{{redemptionCode.wechatTotal ? redemptionCode.wechatUsed+"/"+redemptionCode.wechatTotal:"0/0"}}</text>
					<text style="color: #5F6671;">已使用AI微信兑换码</text>
				</view>
			</view>
			<view class="section margin30">
				<view class="searchVue flex align-center padding-row30">
					<u-icon name="search" size="36" color="#B3B7C0"></u-icon>
					<input type="text" placeholder="搜索兑换码" v-model="inputCode" @input="getSearchRedemptionCode()" />
				</view>
				<u-dropdown class="u-dropdown">
					<u-dropdown-item v-model="activeState" :title="activeState" :options="options1" @change="getRedemptionCodeList()"></u-dropdown-item>
					<u-dropdown-item v-model="redemptionType" :title="redemptionType" :options="options2" @change="getRedemptionCodeList()"></u-dropdown-item>
				</u-dropdown>
				<view class="w100 flex-column align-center padding-column20" v-if="isEmpty">
					<image src="https://ant-aiet.oss-cn-hangzhou.aliyuncs.com/assets/empty.png" style="width: 256rpx; height: 256rpx;"></image>
					<text style="color: #81868e;">{{ describe }}</text>
				</view>
				<view class="item margin40 flex-between align-center"
					v-for="(item, index) in redemptionCodeList" :key="index">
					<view class="flex-column">
						<view class="flex align-center">
							<view :class="[item.type==='AI_WECHAT' ? 'redemptionWeChatLabel' : 'redemptionVideoLabel']">{{item.type==="AI_WECHAT"?'微信':'视频'}}</view>
							<view class="name">{{ item.code }}</view>
							<view class="state">{{ item.used ? '已使用' : '未使用' }}</view>
						</view>
					</view>
					<view class="copy" @click="$tools.copy(item.code)">复制</view>
				</view>
			</view>
		</template>
		
		<template v-if="current===1">
			<view class="box margin30">
				<view class="title">采购兑换码</view>
				<view class="margin20">
					<view style="color: #333;">AI视频兑换码</view>
					<view class="flex-between align-center margin10">
						<text>价格：980元/年</text>
						<u-number-box v-model="purchaseSpNumber"></u-number-box>
					</view>
				</view>
				<view class="margin30">
					<view style="color: #333;">AI微信兑换码</view>
					<view class="flex-between align-center margin10">
						<text>价格：980元/年</text>
						<u-number-box v-model="purchaseWxNumber"></u-number-box>
					</view>
				</view>
				<view class="flex-between margin30">
					<view style="color: #333;">总金额</view>
					<text style="font-weight: 600; font-size: 34rpx; color: #000;">￥{{(purchaseSpNumber+purchaseWxNumber)*980}}</text>
				</view>
				<view class="btn margin20" @click="buyFun()">支付</view>
			</view>
		</template>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	export default {
		data(){
			return {
				tabs: [{ name: "生成兑换码" }, { name: "采购兑换码" }],
				current: 0,
				spNumber: 1,
				wxNumber: 1,
				totalNumber: 0,
				inputCode: '',
				isEmpty: false,
				redemptionCode: {},
				redemptionCodeList: [],
				redemptionType: 'AI视频',
				activeState: '未使用',
				describe: '暂无',
				options1: [{ label: '未使用', value: '未使用' }, { label: '已使用', value: '已使用' }],
				options2: [{ label: 'AI视频', value: 'AI视频' }, { label: 'AI微信', value: 'AI微信' }],
				purchaseWxNumber: 0,
				purchaseSpNumber: 0,
				redemptionCodeList: [],
			}
		},
		computed: {
			...mapState({
				user: state => state.user,
			})
		},
		onLoad() {
			this.getRedemptionCodeList()
			setTimeout(() => {
				this.getRedemptionCode()
			}, 500)
		},
		methods: {
			change(e) {
				this.current = e
			},
			
			// 获取兑换码统计
			async getRedemptionCode() {
				const res = await this.$request("/aiet-user-center/user/redemptionCode/summary", {
					creatorId: this.user.id,
				}, "GET")
				if (res.code === 200) {
					this.redemptionCode = res.data;
					let total = res.data.redemptionCodeNum - ( res.data.wechatTotal +  res.data.videoTotal)
					this.totalNumber =  total> 0 ? total : 0
					this.spNumber = this.totalNumber >= 1 ? 1 : 0 
					this.wxNumber = this.totalNumber -1 < 1 ? 0 : 1
				}
				console.log("获取兑换码统计", this.redemptionCode)
			},
			
			// 获取兑换码列表
			async getRedemptionCodeList() {
				const res = await this.$request("/aiet-user-center/user/redemptionCode/list", {
					creatorId: this.user.id,
					used: this.activeState === '未使用' ? false : true,
					type: this.redemptionType === 'AI微信' ? 'AI_WECHAT' : 'AI_VIDEO'
				}, "GET")
				if (res.code === 200) {
					this.redemptionCodeList = res.data
					this.isEmpty = res.data.length === 0
					this.describe = '暂无' + this.redemptionType + this.activeState + '兑换码'
				}
				console.log("获取兑换码列表", this.redemptionCodeList)
			},
			
			// 生成兑换码
			async generateRedemptionCode() {
				if (this.spNumber < 1 && this.wxNumber < 1) return this.$u.toast("请选择生成数量")
				const res = await this.$request("/aiet-user-center/user/redemptionCode", {
					aiVideo: this.spNumber,
					aiWechat: this.wxNumber
				}, "POST")
				if (res.code === 200) {
					this.$u.toast("生成兑换码成功")
					this.getRedemptionCode()
					this.getRedemptionCodeList()
				} else {
					this.$u.toast(res.msg)
				}
				console.log("获取兑换码统计", this.redemptionCode)
			},
			
			// 立即付款
			async buyFun() {
				if(this.purchaseSpNumber===0 && this.purchaseWxNumber===0) return this.$u.toast("请选择购买数量")
				uni.showLoading()
				const res = await this.$request("/aiet-user-center/user/orders/wxPay", {
					productName: "购买"+this.purchaseSpNumber+"个视频兑换码和"+this.purchaseWxNumber+"个微信兑换码",
					orderAmount: (this.purchaseSpNumber + this.purchaseWxNumber) * 988,
					platform: "数字人-H5",
					productType: 3,
					videoRedemptionCodeNum: this.purchaseSpNumber,
					wechatRedemptionCodeNum: this.purchaseWxNumber,
					manufacturer: 1
				}, "POST", "application/json")
				if (res.code === 200) {
					console.log(res.data)
					uni.hideLoading()
					let payment = res.data
					let that = this
					uni.requestPayment({
						timeStamp: payment.timeStamp,
						nonceStr: payment.nonceStr,
						package: payment.package,
						signType: payment.signType,
						paySign: payment.paySign,
						success(payRes) {
							that.$u.toast("充值成功")
							that.getPoint()
							that.quantity = ''
						},
						fail(payErr) {
							console.log("支付失败", payErr)
						}
					})
				}else {
					uni.hideLoading()
					this.$u.toast(res.msg)
				}
			},
		}
	}
</script>

<style lang="less" scoped>
	.u-dropdown__content__mask {
		background: transparent !important;
	}
	.tabVue {
		width: 690rpx;
	}
	.box {
		width: 690rpx;
		padding: 30rpx;
		background: #fff;
		border-radius: 12rpx;
		.title {
			color: #000;
			font-weight: 600;
		}
		.btn {
			width: 100%;
			height: 80rpx;
			background: #18AB4E;
			line-height: 80rpx;
			text-align: center;
			color: #fff;
			font-size: 30rpx;
			border-radius: 12rpx;
		}
	}
	.info {
		width: 330rpx;
		height: 140rpx;
		border-radius: 12rpx;
		background: #fff;
	}
	.section {
		width: 100%;
		padding: 30rpx;
		background: #fff;
		border-radius: 12rpx;
	
		.label {
			font-weight: 600;
			font-size: 32rpx;
	
			text {
				color: #9EA7A8;
			}
		}
		input {
			font-size: 28rpx;
			padding-left: 20rpx;
		}
		.searchVue {
			width: 100%;
			height: 80rpx;
			background: #F3F4F6;
			border-radius: 40rpx;
		}
		.item {
			width: 100%;
			.copy {
				width: 110rpx;
				height: 60rpx;
				background: #4E80EE;
				border-radius: 10rpx;
				display: flex;
				font-weight: 600;
				justify-content: center;
				align-items: center;
				font-size: 24rpx;
				line-height: 72rpx;
				text-align: center;
				color: #fff;
			}
	
			.name {
				font-weight: 600;
				color: #000;
				font-size: 30rpx;
				line-height: 1;
			}
	
			.redemptionWeChatLabel {
				padding: 6rpx 10rpx;
				font-size: 20rpx;
				background-color: #48D685;
				border-radius: 6rpx;
				color: #FFFFFF;
				margin-right: 12rpx;
			}
	
			.redemptionVideoLabel {
				padding: 6rpx 10rpx;
				font-size: 20rpx;
				background-color: #3C87EF;
				border-radius: 6rpx;
				color: #FFFFFF;
				margin-right: 12rpx;
			}
	
			.state {
				padding-left: 20rpx;
				color: #E47A77;
				font-weight: 600;
			}
	
			.phone {
				line-height: 1;
				color: #333;
				padding-top: 15rpx;
			}
		}
	}
</style>